package com.yzg.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author misterWei
 * @create 2020年04月12号:01点47分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.commit")
public class Thread2Commit {

    public static void main(String[] args) throws InterruptedException {


        Thread thread = new Thread(()->{

            Thread childThread = Thread.currentThread();
            while (true){
                if (childThread.isInterrupted()){
                    log.debug("正常打断");
                        break;
                }

                try {
                    Thread.sleep(1000);
                    log.debug("睡眠后");
                } catch (InterruptedException e) {
                    log.debug("睡眠后被打断");
                    childThread.interrupt();

                }
            }

        },"t1");

        thread.start();
        TimeUnit.SECONDS.sleep(2);
        log.debug("主线程执行完毕");
        //打断操作
        thread.interrupt();

    }


}
